package com.zgm.ch04._4_1;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StopWatch;

import com.zgm.ch04._4_1.interfaces.DemoBean;

public class LookupDemo {

	public static void main(String[] args){
		XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/zgm/ch04/_4_1/_4_1-context.xml"));
		
		stressTest(factory, "abstractLookupDemoBean");
		stressTest(factory, "standardLookupDemoBean");
		
		stressTest(factory, "beanFactoryAwareLookupDemoBean");
	}
	
	public static void stressTest(XmlBeanFactory factory, String beanName){
		DemoBean bean = (DemoBean) factory.getBean(beanName);
		MyHelper help1 = bean.getMyHelper();
		MyHelper help2 = bean.getMyHelper();
		
		System.out.println("测试" + beanName);
		System.out.println("Helper实例是一样的吗？" + (help1 == help2));
		
		StopWatch stopWatch = new StopWatch();
		stopWatch.start("lookupDemo");
		
		for(int i = 0; i < 100000; i++){
			MyHelper helper = bean.getMyHelper();
			helper.doSomethingHelpful();
		}
		
		stopWatch.stop();
		
		System.out.println("10000次获取花费 " + stopWatch.getTotalTimeMillis() + " ms");
	}
}
